package com.geeyuu.android.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Random;

public class CommonUtil {

    private static final String TAG = "CommonUtil";

    public static boolean isEmpty(String str) {
        if (str == null)
            return true;
        if (str.length() == 0)
            return true;
        if (str.trim().length() == 0)
            return true;
        return false;
    }

    public static String getLocalIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException e) {
            LogUtil.e(TAG, "WifiPreference IpAddress", e);
        }
        return null;
    }

    public static boolean isEmail(String strEmail) {
        String strPattern = "^([a-z0-9A-Z]+[\\-_\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        return strEmail.matches(strPattern);
    }

    public static boolean isPwd(String param) {
        String strPattern = "/^(\\w){6,32}$/";
        return param.matches(strPattern);
    }

    public static String getMD5(String str) {
        if (isEmpty(str)) {
            return str;
        }
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();
            byte[] byteArray = messageDigest.digest(str.getBytes("UTF-8"));
            StringBuilder md5StrBuff = new StringBuilder();
            int length = byteArray.length;
            for (int i = 0; i < length; i++) {
                if (Integer.toHexString(0xFF & byteArray[i]).length() == 1){
                    md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
                }else{
                    md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
                }
            }
            return md5StrBuff.toString().toUpperCase();
        } catch (Exception e) {
            LogUtil.e(TAG, "MD5加密异常", e);
        }
        return str;
    }
    
    /**
     * 返回格式 2012-10-10 15:30
     * @return
     */
    public static String getCurrentTimeStr(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(new Date());
    }
    
    private static final String[] s26 = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
    public static String getRandomStr(){
        String s = "";
        Random romRandom = new Random();
        for(int i=0; i<4; i++){
            s = s + s26[romRandom.nextInt(26)];
        }
        s = s + romRandom.nextInt(1000);
        return s;
    }

}
